/*
Copyright 2010 Peter Krajnc, SCREENWORKER

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.screenworker.compactmvc.extensions.factoryservice.resourcetypes
{
	import com.screenworker.compactmvc.extensions.factoryservice.events.FactoryServiceEvent;
	import com.screenworker.compactmvc.extensions.interfaces.IResourceType;
	
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.ByteArray;

	public class ResourceByteArray extends EventDispatcher implements IResourceType
	{
		private var _progress:Number = 0;
		private var _name:String;
		private var _assetLoader:Loader;
		private var _assetBytesClass:ByteArray;
		private var _isComplete:Boolean;
		private var _applicationDomain:ApplicationDomain;
		private var _context:LoaderContext;
		
		public function ResourceByteArray(name:String, assetBytesClass:ByteArray, context:LoaderContext=null)
		{
			super();
			_name = name;
			_isComplete = false;
			_assetBytesClass = assetBytesClass;	
			_context = context;
		}
		
		public function reset():void
		{
			_applicationDomain = null;
			_assetBytesClass = null;
			_assetLoader = null;
		}
		
		private function ioErrorHandler( event:IOErrorEvent ):void		
		{
			dispatchEvent(new FactoryServiceEvent(FactoryServiceEvent.ERROR, resourceName, event.text) );
		}
		
		private function progressHandler(e:ProgressEvent):void
		{
			_progress = int(e.bytesLoaded / e.bytesTotal);
		}
		
		private function completeHandler( event:Event ):void
		{
			_applicationDomain = _assetLoader.contentLoaderInfo.content.loaderInfo.applicationDomain;
			
			_assetLoader = null;
			_isComplete = true;
			dispatchEvent(new FactoryServiceEvent(FactoryServiceEvent.COMPLETE, resourceName) );
		}
		
		public function startLoading():void
        {
        	_assetLoader = new Loader();
			_assetLoader.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true);
			_assetLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, completeHandler , false, 0, true);
			_assetLoader.contentLoaderInfo.addEventListener( ProgressEvent.PROGRESS, progressHandler, false, 0, true);
        	_assetLoader.loadBytes( _assetBytesClass, _context);
        }
        
        public function stopLoading():void
        {
        	_assetLoader.close();
        	_assetLoader = null;
        }
        
		public function get progress():Number
        {
        	return _progress;
        }
        
        public function set progress(value:Number):void
		{
			_progress = value;
		}
        
        public function get resourceName():String
        {
        	return _name;
        }
        
        public function get isComplete():Boolean
        {
        	return _isComplete;
        }
        
        public function set isComplete(value:Boolean):void
        {
        	if(value)
        		stopLoading();
        	_isComplete = value;
        }
        
        public function get classReference():Class 
       	{
       		return null;
       	}
       	
       	public function get applicationDomain():ApplicationDomain
       	{
       		return _applicationDomain;
       	}
	}
}
